package org.example.controller.market;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.dto.market.CKindDto;
import org.example.dao.entity.market.CCommodity;
import org.example.dao.entity.market.CImage;
import org.example.dao.entity.market.CKind;
import org.example.dao.vo.CImageVo;
import org.example.dao.vo.CommodityKindVo;
import org.example.dao.vo.QueryCommodityVo;
import org.example.dao.vo.QueryKindVo;
import org.example.service.market.CKindService;
import org.example.service.market.CommodityService;
import org.example.service.market.StoreService;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/market")
@CrossOrigin
public class CommodityController {

    @Resource
    private CommodityService commodityService;

    @Resource
    private StoreService storeService;

    @Resource
    private CKindService cKindService;

    /**
     * 添加商品(商户)
     */
    @PostMapping("addCommodity")
    @HasRole(role = "商户")
    public Result addCommodity(@RequestBody CommodityKindVo commodityKindVo){
        CCommodity commodity = commodityKindVo.getCommodity();
        List<Long> kindIds = commodityKindVo.getKindIds();
        Long storeId = commodity.getStoreId();
        ParamUtil.LongParam(storeId);
        storeService.checkStore(storeId);
        commodity = commodityService.addCommodity(kindIds, commodity);
        return new Result(ResultCode.SUCCESS, commodity);
    }

    /**
     * 查询该商店有哪些商品分类(商户用户通用)
     */
    @PostMapping("selectKindByStore/{limit}/{start}")
    public Result selectKindByStore(@PathVariable("limit") Long limit,
                                    @PathVariable("start") Long start,
                                    @RequestBody QueryKindVo queryKindVo) {
        Long storeId = queryKindVo.getStoreId();
        Long fatherId = queryKindVo.getFatherId();
        ParamUtil.LongParam(storeId);
        ParamUtil.LongParam(fatherId);
        Page<CKindDto> cKindPage = cKindService.selectKindByStore(limit, start, storeId, fatherId);
        return new Result(ResultCode.SUCCESS, cKindPage);
    }

    /**
     * 查询所有商品分类(管理员)
     */
    @PostMapping("selectAllKind/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectAllKind(@PathVariable("limit") Long limit,
                                @PathVariable("start") Long start,
                                @RequestBody QueryKindVo queryKindVo) {
        Long fatherId = queryKindVo.getFatherId();
        ParamUtil.LongParam(fatherId);
        Page<CKindDto> cKindIPage = cKindService.selectAllKind(limit, start, fatherId);
        return new Result(ResultCode.SUCCESS, cKindIPage);
    }

    /**
     * 查询商店有哪些商品符合商品类型(商户用户通用)
     */
    @PostMapping("selectCommodityByKind/{limit}/{start}")
    public Result selectCommodityByKind(@PathVariable("limit") Long limit,
                                        @PathVariable("start") Long start,
                                        @RequestBody QueryCommodityVo queryCommodityVo) {
        Long storeId = queryCommodityVo.getStoreId();
        Long kindId = queryCommodityVo.getKindId();
        ParamUtil.LongParam(storeId);
        ParamUtil.LongParam(kindId);
        IPage<CCommodity> cCommodityIPage = commodityService.selectCommodityByKind(storeId, kindId, limit, start);
        return new Result(ResultCode.SUCCESS, cCommodityIPage);
    }

    /**
     * 查询有哪些商品符合商品类型(管理员)
     */
    @PostMapping("selectAllCommodityByKind/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectAllCommodityByKind(@PathVariable("limit") Long limit,
                                           @PathVariable("start") Long start,
                                           @RequestBody QueryCommodityVo queryCommodityVo) {
        Long kindId = queryCommodityVo.getKindId();
        ParamUtil.LongParam(kindId);
        IPage<CCommodity> cCommodityIPage = commodityService.selectCommodityByKind(null, kindId, limit, start);
        return new Result(ResultCode.SUCCESS, cCommodityIPage);
    }

    /**
     * 查询该商店有哪些商品(商户用户通用)
     */
    @PostMapping("selectCommodityByStore/{limit}/{start}")
    public Result selectCommodityByStore(@PathVariable("limit") Long limit,
                                         @PathVariable("start") Long start,
                                         @RequestBody CCommodity commodity){
        Long storeId = commodity.getStoreId();
        ParamUtil.LongParam(storeId);
//        storeService.checkStore(storeId);
        IPage<CCommodity> cCommodityIPage = commodityService.selectCommodityByStore(limit, start, storeId);
        return new Result(ResultCode.SUCCESS, cCommodityIPage);
    }

    /**
     * 查询所有商品(管理)
     */
    @PostMapping("selectCommodity/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectCommodity(@PathVariable("limit") Long limit,
                                  @PathVariable("start") Long start){
        IPage<CCommodity> cCommodityIPage = commodityService.selectCommodity(limit, start);
        return new Result(ResultCode.SUCCESS, cCommodityIPage);
    }

    /**
     * 条件查询商品(商户)
     */
    @PostMapping("selectCommodityByCondition/{limit}/{start}")
    public Result selectCommodityByCondition(@PathVariable("limit") Long limit,
                                             @PathVariable("start") Long start){

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 修改商品信息(商户)
     */
    @PostMapping("updateCommodity")
    @HasRole(role = "商户")
    public Result updateCommodity(@RequestBody CCommodity commodity){
        Long id = commodity.getId();
        ParamUtil.LongParam(id);
        commodity = commodityService.updateCommodity(commodity);
        return new Result(ResultCode.SUCCESS, commodity);
    }

    /**
     * 修改商品的类型(商户)
     */
    @PostMapping("updateCommodityKind")
    @HasRole(role = "商户")
    public Result updateCommodityKind(@RequestBody CommodityKindVo commodityKindVo){
        Long id = commodityKindVo.getCommodity().getId();
        ParamUtil.LongParam(id);
        commodityKindVo = commodityService.updateCommodityKind(commodityKindVo);
        return new Result(ResultCode.SUCCESS, commodityKindVo);
    }

    /**
     * 修改商品主页图标(商户)
     */
    @PostMapping("updateCommodityIcon/{commodityId}")
    @HasRole(role = "商户")
    public Result updateCommodityIcon(@PathVariable("commodityId")Long commodityId, MultipartFile file){
        ParamUtil.LongParam(commodityId);
        CCommodity cCommodity = commodityService.updateCommodityIcon(commodityId, file);
        return new Result(ResultCode.SUCCESS, cCommodity);
    }

    /**
     * 上传店铺图像(商户)
     */
    @PostMapping("uploadCommodityImages/{commodityId}")
    @HasRole(role = "商户")
    public Result uploadCommodityImages(@RequestBody MultipartFile[] files, @PathVariable Long commodityId) {
        ParamUtil.LongParam(commodityId);
        List<CImage> cImageList = commodityService.uploadCommodityImages(files, commodityId);
        return new Result(ResultCode.SUCCESS, cImageList);
    }

    /**
     * 查看商品图像(商户用户通用)
     */
    @PostMapping("selectCommodityImages")
    public Result selectCommodityImages(@RequestBody CImageVo cImageVo) {
        Long commodityId = cImageVo.getCommodityId();
        ParamUtil.LongParam(commodityId);
        List<CImage> cImageList = commodityService.selectCommodityImages(commodityId);
        return new Result(ResultCode.SUCCESS, cImageList);
    }

    /**
     * 删除商品(商户)
     */
    @PostMapping("/deleteCommodity")
    @HasRole(role = "商户")
    public Result deleteCommodity(@RequestBody CCommodity commodity){
        Long id = commodity.getId();
        ParamUtil.LongParam(id);
        commodityService.deleteCommodity(commodity);
        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 添加商品类型(管理)
     */
    @PostMapping("addKind")
    @HasRole(role = "管理员")
    public Result addKind(@RequestBody CKind kind){
        kind = cKindService.addKind(kind);
        return new Result(ResultCode.SUCCESS, kind);
    }

    /**
     * 删除商品类型(管理)
     */
    @PostMapping("deleteKind")
    @HasRole(role = "管理员")
    public Result deleteKind(@RequestBody CKind kind){
        cKindService.deleteKind(kind);
        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 修改商品类型(管理)
     */
    @PostMapping("updateKind")
    @HasRole(role = "管理员")
    public Result updateKind(@RequestBody CKind kind){
        kind = cKindService.updateKind(kind);
        return new Result(ResultCode.SUCCESS, kind);
    }

    /**
     * 查询商品类型(管理)
     */
    @PostMapping("selectKind/{limit}/{start}")
    @HasRole(role = "管理员")
    public Result selectKind(@PathVariable("limit") Long limit,
                             @PathVariable("start") Long start){
        IPage<CKind> cKindIPage = cKindService.selectKind(limit, start);
        return new Result(ResultCode.SUCCESS, cKindIPage);
    }

    /**
     * 导出导入模板(商户)
     */
    @PostMapping("downloadCommodity")
    public Result downloadCommodity(){

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 按模板导入商品(商户)
     */
    @PostMapping("importCommodity")
    public Result importCommodity(){

        return new Result(ResultCode.SUCCESS, null);
    }

}
